package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.xutils.x;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.GuestsseatBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.RegularUtil;
import a66weding.com.jiehuntong.common.utils.SharesUtils;
import a66weding.com.jiehuntong.common.utils.Tools;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/7/22.
 * 头部 结婚计划书 --结婚预算;
 */
public class Diy_YusuanPagerActivity extends Activity implements OnNetResultListener ,View.OnClickListener{
    public static String MARRY_BUDGET = "marry_budget";
    private EditText et_editmoney;
    private Button yusuan_finish_btn;
    private  String  money;
    private static final int DIALOGDISSMISS=2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_diy_yusuan);
        money = getIntent().getStringExtra("Diy_HeadActivity");
        initView();
    }

    private  void  initView(){
        et_editmoney = (EditText) findViewById(R.id.yusuan_editmoney_et);
        et_editmoney.setText(money);
        if (!TextUtils.isEmpty(money)){
            et_editmoney.setSelection(money.length());
        }
        yusuan_finish_btn = (Button) findViewById(R.id.yusuan_finish_btn);
        yusuan_finish_btn.setOnClickListener(this);
    }

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case DIALOGDISSMISS:
                    Tools.dismissWaitDialog();
                    break;

            }
        }
    };


    public void backBtn(View view){
        Intent intent = new Intent();
        if (RegularUtil.isNonzeroOfinteger(money)){
            intent.putExtra(MARRY_BUDGET,money);
            setResult(4,intent);
            finish();
        }else{
            intent.putExtra(MARRY_BUDGET,"");
            setResult(4,intent);
            finish();
        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Tools.dismissWaitDialog();
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    /**
     * 提交结婚预算;
     */
    private void upDateBudget() {
        HashMap<String, String> mapData = new HashMap<String, String>();
        money = et_editmoney.getText().toString();
        mapData.put("WeddingBudget", money);
        String userUrl = UrlDealUtil.getRealUrl(RequestCode.UPDATE_WEDDINGBUDGET);
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, userUrl, this, GuestsseatBean.class, RequestCode.UPDATE_WEDDINGBUDGET, mapData);
        HttpHandleUtil.executeCommonPost(this, requestBean);
        Tools.showDialog(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.yusuan_finish_btn:
               money=  et_editmoney.getText().toString();
                if (RegularUtil.isNonzeroOfinteger(money)){
                    upDateBudget();
                   saveshareXml(MARRY_BUDGET);
                    Intent intent = new Intent();
                    intent.putExtra(MARRY_BUDGET,money);
                    setResult(4,intent);
                    finish();
                }else{
                    Tools.showResultDialogSmall(this,"请输入大于0的正数",true);
                    mHandler.sendEmptyMessageDelayed(DIALOGDISSMISS,1000);
                }
                break;
        }
    }

    private void saveshareXml(String name){
        SharesUtils.getInstance(this).writeXML( name,money);
        Toast.makeText(this,"保存数据成功",Toast.LENGTH_SHORT).show();
    }
}
